from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from app import app

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@127.0.0.1:3306/test'

# app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + "D:\\devFront\\PythonProject\\database\\test.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
app.config["SECRET_KEY"] = "ljp"
app.config["SQLALCHEMY_POOL_RECYCLE"] = 10
app.config["SQLALCHEMY_POOL_SIZE"] = 100

# print(app)
db = SQLAlchemy(app)

# 用户表
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(12), nullable=False, unique=True)  # 用户名独一无二
    password = db.Column(db.String(12), nullable=False)
    hasMeasure = db.Column(db.Boolean, default=False)
    deviceUploadData_id = db.relationship("DeviceUploadData", backref="testee")  # 关联上传数据表
    oneDimensionData_id = db.relationship("OneDimensionData", backref="testee")  # 关联一维数据表
    twoDimensionData_id = db.relationship("TwoDimensionData", backref="testee")  # 关联二维数据表
    oneDimensionDataFromPic_id = db.relationship("OneDimensionDataFromPic", backref="testee")  # 关联图像提取的一维数据表
    modelPathData_id = db.relationship("ModelPathData", backref="testee")

# 测量平台上传数据表
class DeviceUploadData(db.Model):
    __tablename__ = 'deviceUploadData'
    id = db.Column(db.Integer, primary_key=True)
    testee_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属被测者

    # 毫米波部分
    height = db.Column(db.Integer, nullable=False)
    chestWidth = db.Column(db.Integer, nullable=False)
    chestThickness = db.Column(db.Integer, nullable=False)
    waistWidth = db.Column(db.Integer, nullable=False)
    waistThickness = db.Column(db.Integer, nullable=False)
    hipsWidth = db.Column(db.Integer, nullable=False)
    hipsThickness = db.Column(db.Integer, nullable=False)
    chestLength = db.Column(db.Integer, nullable=False)
    waistLength = db.Column(db.Integer, nullable=False)
    hipsLength = db.Column(db.Integer, nullable=False)
    shoulderWidth = db.Column(db.Integer, nullable=False)
    legThickness = db.Column(db.Integer, nullable=False)

    # kinect部分
    headLength = db.Column(db.Integer, nullable=False)
    bodyLength = db.Column(db.Integer, nullable=False)
    armLength = db.Column(db.Integer, nullable=False)
    legLength = db.Column(db.Integer, nullable=False)
    
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间

# 一维数据表
class OneDimensionData(db.Model):
    __tablename__ = 'oneDimensionData'
    id = db.Column(db.Integer, primary_key=True)
    testee_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属被测者
    height = db.Column(db.Integer, nullable=False)
    chestWidth = db.Column(db.Integer, nullable=False)
    chestThickness = db.Column(db.Integer, nullable=False)
    waistWidth = db.Column(db.Integer, nullable=False)
    waistThickness = db.Column(db.Integer, nullable=False)
    hipsWidth = db.Column(db.Integer, nullable=False)
    hipsThickness = db.Column(db.Integer, nullable=False)
    
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间


# 图像中提取的一维数据表
class OneDimensionDataFromPic(db.Model):
    __tablename__ = 'oneDimensionDataFromPic'
    id = db.Column(db.Integer, primary_key=True)
    testee_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    shoulderWidth = db.Column(db.Integer, nullable=False)
    armLength = db.Column(db.Integer, nullable=False)
    legLength = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间


# 二维数据表
class TwoDimensionData(db.Model):
    __tablename__ = 'twoDimensionData'
    id = db.Column(db.Integer, primary_key=True)
    testee_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属被测者
    chest = db.Column(db.Integer, nullable=False)
    waist = db.Column(db.Integer, nullable=False)
    hips = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间


class ModelPathData(db.Model):
    __tablename__ = 'modelPathData'
    id = db.Column(db.Integer, primary_key=True)
    testee_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    path = db.Column(db.String(50), nullable=False, unique=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间


if __name__ == "__main__":
    db.drop_all()
    db.create_all()
    # 用户数据
    s1 = User(username="admin", password="root")
    s2 = User(username="ljp", password="123456")
    s3 = User(username="lxh", password="123456")

    # add:
    db.session.add(s1)
    db.session.add_all([s1, s2, s3])
    db.session.commit()

    # select
    users = User.query.all()
    print(users)

    